package com.ruimei.util;

import java.io.File;
import java.io.PrintStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import org.springframework.util.StringUtils;

public class CommUtil
{
  public static String getUUID()
  {
    return UUID.randomUUID().toString().replaceAll("-", "");
  }
  
  public static String[] getUUID(int number)
  {
    if (number < 1) {
      return null;
    }
    String[] ss = new String[number];
    for (int i = 0; i < number; i++) {
      ss[i] = getUUID();
    }
    return ss;
  }
  
  public static String formatLongDate(Object v)
  {
    if ((v == null) || (v.equals(""))) {
      return "";
    }
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    return df.format(v);
  }
  
  public static String formatShortDate(Object v)
  {
    if (v == null) {
      return null;
    }
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    
    return df.format(v);
  }
  
  public static String formateMiddleDate(Object v)
  {
    if ((v == null) || (v.equals(""))) {
      return "";
    }
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    
    return df.format(v);
  }
  
  public static String GetRandomNum()
  {
    String t = getStringDate();
    int x = (int)(Math.random() * 900000.0D) + 100;
    String serial = t + x;
    return serial;
  }
  
  public static String getStringDate()
  {
    Date currentTime = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
    String dateString = formatter.format(currentTime);
    return dateString;
  }
  
  public static String formatNYR(String time)
  {
    String date = "";
    if ((time != null) && (!time.equals(""))) {
      try
      {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = df.parse(time);
        date = sdf.format(parse);
      }
      catch (ParseException e)
      {
        e.printStackTrace();
      }
    }
    return date;
  }
  
  public static String getStringByDate(Date currentTime)
  {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String dateString = df.format(currentTime);
    return dateString;
  }
  
  public static String getBeginBirthdayOfAge(int age)
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-01-01");
    Calendar rightNow = Calendar.getInstance();
    rightNow.add(1, -age);
    Date dt1 = rightNow.getTime();
    String reStr = sdf.format(dt1);
    return reStr;
  }
  
  public static String getEndBirthdayOfAge(int age)
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-12-31");
    Calendar rightNow = Calendar.getInstance();
    rightNow.add(1, -age);
    Date dt1 = rightNow.getTime();
    String reStr = sdf.format(dt1);
    return reStr;
  }
  
  public static String formatAge(String age)
  {
    String date = "";
    if ((age != null) && (!age.equals("")))
    {
      Calendar calendar = Calendar.getInstance();
      int year = calendar.get(1);
      int month = calendar.get(2);
      int day = calendar.get(5);
      System.out.println("year:" + year + " month:" + (month + 1) + " day:" + day);
      
      int yearOld = year - Integer.valueOf(age).intValue();
      calendar.set(yearOld, month, day);
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      date = sdf.format(calendar.getTime());
    }
    return date;
  }
  
  public static String getAgeFromBirthTime(String birthday)
  {
    String date = "";
    if ((birthday != null) && (!birthday.equals("")))
    {
      String[] strs = birthday.trim().split("-");
      int selectYear = Integer.parseInt(strs[0]);
      int selectMonth = Integer.parseInt(strs[1]);
      int selectDay = Integer.parseInt(strs[2]);
      
      Calendar cal = Calendar.getInstance();
      int yearNow = cal.get(1);
      int monthNow = cal.get(2) + 1;
      int dayNow = cal.get(5);
      
      int yearMinus = yearNow - selectYear;
      int monthMinus = monthNow - selectMonth;
      int dayMinus = dayNow - selectDay;
      
      int age = yearMinus;
      if (yearMinus < 0) {
        age = 0;
      } else if (yearMinus == 0)
      {
        if (monthMinus < 0) {
          age = 0;
        } else if (monthMinus == 0)
        {
          if (dayMinus < 0) {
            age = 0;
          } else if (dayMinus >= 0) {
            age = 1;
          }
        }
        else if (monthMinus > 0) {
          age = 1;
        }
      }
      else if ((yearMinus > 0) && 
        (monthMinus >= 0)) {
        if (monthMinus == 0)
        {
          if ((dayMinus >= 0) && 
            (dayMinus >= 0)) {
            age += 1;
          }
        }
        else if (monthMinus > 0) {
          age += 1;
        }
      }
      date = String.valueOf(age - 1);
    }
    return date;
  }
  
  public static boolean isEmpty(Object obj)
  {
    if (obj == null) {
      return true;
    }
    if ((obj instanceof CharSequence)) {
      return ((CharSequence)obj).length() == 0;
    }
    if ((obj instanceof Collection)) {
      return ((Collection)obj).isEmpty();
    }
    if ((obj instanceof Map)) {
      return ((Map)obj).isEmpty();
    }
    return false;
  }
  
  public static String getSysJournalNo(int length, boolean isNumber)
  {
    String uuid = UUID.randomUUID().toString().replaceAll("-", "");
    if (uuid.length() > length) {
      uuid = uuid.substring(0, length);
    } else if (uuid.length() < length) {
      for (int i = 0; i < length - uuid.length(); i++) {
        uuid = uuid + Math.round(Math.random() * 9.0D);
      }
    }
    if (isNumber) {
      return uuid.replaceAll("a", "1").replaceAll("b", "2").replaceAll("c", "3").replaceAll("d", "4").replaceAll("e", "5").replaceAll("f", "6");
    }
    return uuid;
  }
  
  public static Map<Integer, String> getAuditStatus()
  {
    Map<Integer, String> map = new HashMap();
    map.put(Integer.valueOf(1), "未回复");
    map.put(Integer.valueOf(2), "已回复");
    return map;
  }
  
  public static Map<Integer, String> getRelation()
  {
    Map<Integer, String> map = new HashMap();
    map.put(Integer.valueOf(1), "父亲");
    map.put(Integer.valueOf(2), "母亲");
    map.put(Integer.valueOf(3), "丈夫");
    map.put(Integer.valueOf(4), "妻子");
    map.put(Integer.valueOf(5), "哥哥");
    map.put(Integer.valueOf(6), "弟弟");
    map.put(Integer.valueOf(7), "姐姐");
    map.put(Integer.valueOf(8), "妹妹");
    map.put(Integer.valueOf(9), "其他");
    
    return map;
  }
  
  public static final String randomString(int length)
  {
    char[] numbersAndLetters = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    if (length < 1) {
      return "";
    }
    Random randGen = new Random();
    char[] randBuffer = new char[length];
    for (int i = 0; i < randBuffer.length; i++) {
      randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
    }
    return new String(randBuffer);
  }
  
  public static boolean deleteFile(String fileName)
  {
    File file = new File(fileName);
    if ((file.exists()) && (file.isFile()))
    {
      if (file.delete())
      {
        System.out.println("删除单个文件" + fileName + "成功！");
        return true;
      }
      System.out.println("删除单个文件" + fileName + "失败！");
      return false;
    }
    System.out.println("删除单个文件失败：" + fileName + "不存在！");
    return false;
  }
  
  public static boolean isCurrTime(String time)
  {
    DateFormat df = DateFormat.getDateInstance();
    try
    {
      Date date = df.parse(time);
      Date currTime = df.parse(df.format(new Date()));
      return date.after(currTime);
    }
    catch (ParseException e)
    {
      e.printStackTrace();
    }
    return false;
  }
  
  public static Date getWebsiteDatetime(String webuUrl)
  {
    try
    {
      if (!StringUtils.isEmpty(webuUrl))
      {
        URL url = new URL(webuUrl);
        URLConnection uc = url.openConnection();
        uc.connect();
        long ld = uc.getDate();
        return new Date(ld);
      }
      System.out.println("URL Error!!!");
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return null;
  }
  
  public static Date formatStringToDate(String date)
  {
    Date date1 = null;
    try
    {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      date1 = sdf.parse(date);
    }
    catch (ParseException e)
    {
      e.printStackTrace();
    }
    return date1;
  }
  
  public static String getWeekOfDate(Date dt)
  {
    String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
    Calendar cal = Calendar.getInstance();
    cal.setTime(dt);
    
    int w = cal.get(7) - 1;
    if (w < 0) {
      w = 0;
    }
    return weekDays[w];
  }
  
  public static void main(String[] args)
  {
    String pass = Md5Encrypt.md5(Md5Encrypt.md5("123456") + "cnSnSv");
    System.out.println(pass);
  }
}


/* Location:              E:\360MoveData\Users\admin\Desktop\teacher_wap-0.0.1-SNAPSHOT.jar!\BOOT-INF\classes\com\ruime\\util\CommUtil.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       0.7.1
 */